package com.zw.mes.module.shipments.controller;


import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.shipments.InstShipmentsPlanSheet;
import com.zw.mes.entity.shipments.InstShipmentsProductDetail;
import com.zw.mes.entity.shipments.InstShipmentsPurchasedDetail;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.prodtaskmanager.entity.VwProject;
import com.zw.mes.module.shipments.model.LinshiProductDetail;
import com.zw.mes.module.shipments.repository.PlanSheetRepository;
import com.zw.mes.module.shipments.service.InstShipmentsPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;


/**
 * @Description: 创建发货计划
 * <p>
 * 权限：
 * @Author: liqingyu
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/create/shipments")
@SuppressWarnings("all")
public class InstShipmentsPlanController extends BaseController {


    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private InstShipmentsPlanService instShipmentsPlanService;
    @Autowired
    private PlanSheetRepository planSheetRepository;


    @GetMapping("/{id}")
//    @RequiresPermissions({"tec:proDetail:view"})
    public String page(Model model,@PathVariable("id") Integer id) {
        model.addAttribute("specialId", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "shipments/InstShipmentsPlan/page";
    }

    /**
     * 计划单主表
     */
    @ResponseBody
    @PostMapping("/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult list(Model model) {
        startPage();
        List<InstShipmentsPlanSheet> list = instShipmentsPlanService.selectShipmentsList();
        return jsonResult(list);
    }

    /**
     * 查询项目
     *
     * @param q
     * @return
     */
    @GetMapping("/project/list")
    @ResponseBody
    public List<VwProject> proList(String q) {
        List<VwProject> mm= instShipmentsPlanService.findProjectByCode(q);
        return instShipmentsPlanService.findProjectByCode(q);
    }

    /**
     * 发货计划查询
     * @param model
     * @param id
     * @return
     */
    @GetMapping("shipmentsSelect")
    public String shipmentsSelect(Model model) {
        return "shipments/InstShipmentsPlanSelect/page";
    }

//-- SELECT a.prod_no as pro_code,a.prod_num as pro_qty,b.pro_id,b.customer,b.pro_name FROM (
//-- SELECT prod_pro_cod,prod_no,prod_num FROM prod_ord where prod_no in (
//            -- select DISTINCT prod_no from  asse_half_store where  prod_no like '%' + #{proCode} + '%' and num>0
//            -- UNION
//-- SELECT DISTINCT prod_no FROM asse_store
//--  where  prod_no like '%' + #{proCode} + '%' and draw_num>0)) a LEFT JOIN vw_project b on a.prod_pro_cod = b.pro_code

    /**
     * 增加发货计划主单
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/add/PlanSheet")
//    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult addProduction(InstShipmentsPlanSheet instShipmentsPlanSheet,String[] drawNo,  String[] drawName, String[] drawNum, String[] unit, String[] location,String[] remark) {
        InstShipmentsPlanSheet instShipmentsPlanSheet1=  instShipmentsPlanService.saveInstShipmentsPlanSheet(instShipmentsPlanSheet,drawNo,drawName,drawNum,unit,location,remark);

        return JsonResult.success("操作成功",instShipmentsPlanSheet1);
    }
    /**
     * 修改发货计划主单
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/edit/PlanSheet")
//    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult editProduction(InstShipmentsPlanSheet instShipmentsPlanSheet,String[] drawNo,  String[] drawName, String[] drawNum, String[] unit, String[] location,String[] remark) {
        InstShipmentsPlanSheet instShipmentsPlanSheet1=  instShipmentsPlanService.saveInstShipmentsPlanSheetEdit(instShipmentsPlanSheet,drawNo,drawName,drawNum,unit,location,remark);

        return JsonResult.success("操作成功",instShipmentsPlanSheet1);
    }
    /**
     * 根据主表ID查询制作件明细表
     */
    @ResponseBody
    @PostMapping("/prodectDetail")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult prodectDetail(Model model, InstShipmentsProductDetail instShipmentsProductDetail) {
        startPage();
        List<InstShipmentsProductDetail> list = instShipmentsPlanService.selectProductDetail(instShipmentsProductDetail);
        return jsonResult(list);
    }
    /**
     * 根据主表ID查询外购件明细
     */
    @ResponseBody
    @PostMapping("/addPurch")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult PurchDetail(Model model, InstShipmentsProductDetail instShipmentsProductDetail) {
        startPage();
        List<InstShipmentsPurchasedDetail> list = instShipmentsPlanService.selectPurchDetail(instShipmentsProductDetail);
        return jsonResult(list);
    }

    /**
     * 根据主表ID查询制作件明细表
     */
    @ResponseBody
    @PostMapping("/productDetailList")
    //    @RequiresPermissions({"report:query:view"})
    public JsonResult productDetailList(String proCode) {
        startPage();
        List<LinshiProductDetail> list = instShipmentsPlanService.selectLinshiProductDetail(proCode);
        return jsonResult(list);
    }

    /**
     * 保存制作件发货明细
     */
    @ResponseBody
    @PostMapping("/shipProduct")
    //    @RequiresPermissions({"report:query:view"})
    public JsonResult shipProduct(String arr, String sheetId) {
        Gson gson = new Gson();
        List<LinshiProductDetail> prodPlaDetList = gson.fromJson(arr, new TypeToken<List<LinshiProductDetail>>() {
        }.getType());
        instShipmentsPlanService.saveProductList(prodPlaDetList, sheetId);
        return jsonResult(1);
    }

    /**
     * 跳转选择制作件页面
     */
    @GetMapping("/addProduct")
//    @RequiresPermissions({"dsn:proBull:view"})
    public String tzrwList(Model model, String sheetId) {
        InstShipmentsPlanSheet instShipmentsPlanSheet = planSheetRepository.getOne(Integer.valueOf(sheetId));
        model.addAttribute("sheetId", sheetId);
        model.addAttribute("proCode",instShipmentsPlanSheet.getProCode() );
        return "shipments/InstShipmentsPlan/addProductPage";
    }
    /**
     * 保存外购件发货明细
     */
    @ResponseBody
    @PostMapping("/addPurchList")
    //    @RequiresPermissions({"report:query:view"})
    public JsonResult addPurchList(InstShipmentsPurchasedDetail instShipmentsPurchasedDetail, String[] drawName, String[] drawNum, String[] unit, String[] location,String[] remark) {
        instShipmentsPlanService.savePurchList(instShipmentsPurchasedDetail.getSheetId(),drawName,drawNum,unit,location,remark);
        return jsonResult(1);
    }
    /**
     * 提交主单
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/submit")
//    @RequiresPermissions({"dsn:proTasks:view"})
    public JsonResult submit(Model model,Integer id) {
        int sub= instShipmentsPlanService.updateByIdSubmit(id);
        return jsonResult(1);
    }
    /**
     * 删除主单
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/delMain")
//    @RequiresPermissions({"dsn:proTasks:view"})
    public JsonResult delMain(Model model,Integer id) {
        int del= instShipmentsPlanService.updateByIdDel(id);
        return jsonResult(1);
    }
    /**
     * 制作件明细单
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/delProduct")
//    @RequiresPermissions({"dsn:proTasks:view"})
    public JsonResult delProduct(Model model,Integer id) {
        int del= instShipmentsPlanService.updateByIdDelProduct(id);
        return jsonResult(1);
    }
}
